#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  for (int cas = 1; cas <= T; cas ++) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i ++) cin >> a[i];
    vector<vector<int>> dp(n + 1, vector<int>(n + 1));
    vector<vector<vector<int>>> f(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, INT_MIN / 2)));
    for (int l = n; l >= 1; l --) {
      dp[l][l] = 1;
      f[l][l][1] = 0;
      for (int r = l + 1; r <= n; r ++) {
        for (int k = l; k < r; k ++) {
          dp[l][r] = max(dp[l][r], dp[l][k] + dp[k + 1][r]);
        }

        if (a[l] == a[r]) {
          for (int k = l; k < r; k ++) {
            if (a[k] == a[l]) {
              for (int c = 2; c <= r - l + 1; c ++) {
                f[l][r][c] = max(f[l][r][c], f[l][k][c - 1] + dp[k + 1][r - 1]);
              }
            }
          }
          for (int c = 2; c <= r - l + 1; c ++) {
            dp[l][r] = max(dp[l][r], f[l][r][c] + c * c);
          }
        }
      }
    }
    cout << "Case " << cas << ": " << dp[1][n] << endl;
  }
}